package com.spring.demo.core.rmi;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.spring.demo.core.entity.User;
import com.spring.demo.core.service.ICacheService;

/**
 * {@link IRMIUserService} impl
 * 
 * @author sean
 * 
 */
public class RMIUserService implements IRMIUserService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ICacheService cacheService;

	@Override
	public boolean addUser(User user) {
		cacheService.getUserMap().put(user.getId(), user);
		logger.debug("User has bean added to cache. User: {}", user);
		return true;
	}

	@Override
	public boolean deleteUser(User user) {
		cacheService.getUserMap().remove(user.getId());
		logger.debug("User has bean deleted from cache. User: {}", user);
		return true;
	}

	@Override
	public List<User> getUserList() {
		List<User> users = new ArrayList<User>();
		users.addAll(cacheService.getUserMap().values());
		logger.debug("User list: {}", users);

		return users;
	}

}
